package cn.jfinal.controller;

import com.jfinal.core.Controller;
import com.jfinal.plugin.activerecord.Db;
import com.jfinal.plugin.activerecord.Record;
import org.apache.log4j.Logger;

import java.util.Map;

/**
 * @packageName: cn.jfinal.controller
 * @name: UserController
 * @description:
 * @author: 董定卓
 * @dateTime: 2018/8/22 15:11
 */
public class UserController extends Controller {

    Logger logger = Logger.getLogger(UserController.class);

    public void index() {
        setAttr("userPage", Db.paginate(getParaToInt(0, 1), 10, "select *", "from t_user where age>10"));
        render("/page/user/index.html");
    }

    public void add() {
        render("/page/user/add.html");
    }

    public void edit() {
        Record user = Db.findById("t_user", getParaToInt());
        setAttr("user", user);
        render("/page/user/edit.html");
    }

    public void save() {
        Record record = new Record().set("name", getPara("name")).set("age", getPara("age"));
        Db.save("t_user", record);
        redirect("/user");
    }

    public void update() {
        Map<String, String[]> paraMap = getParaMap();

        Record record = new Record().set("id",getPara("id")).set("name", getPara("name")).set("age", getPara("age"));
        Db.update("t_user", record);
        redirect("/user");
    }

    public void delete() {
        Db.deleteById("t_user", getParaToInt());
        redirect("/user");
    }


}
